
package cliente;

import elementos.Bar;
import elementos.Composite;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author julio
 */
public class BarController extends Thread implements KeyListener{
    private Bar player;
    private Composite scene;
    private int keyUp, keyDown;
    private boolean stopping = false;
    private boolean computer=false;
    private boolean up = true;
    
    
    public void exit(){
        stopping=true;
    }

    public Bar getPlayer() {
        return player;
    }
    
    public void run(){
        while (!stopping) {
             if(computer){
                try {
                    //ia caja tonta
                    if(up){
                        player.moveUp();
                        if(scene.collision(player)!=null) up=false;
                    }else{
                        player.moveDown();
                        if(scene.collision(player)!=null) up=true;
                    }
                    sleep(20);
                } catch (Exception ex) {
                    Logger.getLogger(BarController.class.getName()).log(Level.SEVERE, null, ex);
                }
             }
        }
    }

    public BarController(Bar player,Composite scene, int keyUp, int keyDown) {
        this.player = player;
        this.scene=scene;
        if(keyUp==keyDown && keyUp==0 && keyDown==0){
            computer=true;
        }else{
            this.keyUp = keyUp;
            this.keyDown = keyDown;
        }
    }

    @Override
    public void keyTyped(KeyEvent ke) {
        if(!computer){
            if (ke.getKeyCode() == keyUp) {
                if (scene.collision(player) == null) {
                    player.moveUp();
                }
            }
            if (ke.getKeyCode() == keyDown) {
                if (scene.collision(player) == null) {
                    player.moveDown();
                }
            }
        }
    }

    @Override
    public void keyPressed(KeyEvent ke) {
        if(!computer){
            if (ke.getKeyCode() == keyUp) {
                if (scene.collision(player) == null) {
                    player.moveUp();
                }
            }
            if (ke.getKeyCode() == keyDown) {
                if (scene.collision(player) == null) {
                    player.moveDown();
                }
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent ke) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
}
